var roleManage = function() {
    var storeId = $('input[name="storeId"]').val();
    var groupId = $('input[name="groupId"]').val();;
    var _init = function() {
        var  table = $('#table_record');
        table.dataTable({
            "processing" : true,        
            serverSide : true,// 开启服务器模式
            "ajax" : {
                url : record_url,  // 列表查询
                data:{'storeId':storeId,'groupId':groupId}
            },
            "autoWidth" : false,
            "columns" : [
                {
                    "width" : "15%",
                    "data" : "title",
                    "render" : function(data, type, row, meta) {
                        return '<img src="'+row.imgUrl+'" alt="" style="width:60px;height:60px;" >' + data;
                    }
                },
                {"width" : "5%","data" : "count"},
                {
                    "width" : "10%",
                    "data" : "create_time",
                    "render" : function(data, type, row, meta) {
                        return data;
                    }
                },
                {
                    "width" : "5%",
                    "data" : "status",
                    "render" : function(data, type, row, meta) {
                        if(data == 1) {
                            return '进行中';
                        } else if(data == 2) {
                            return '拼团成功';
                        } else if(data == 3) {
                            return '拼团失败';
                        } else {
                            return '';
                        }
                    }
                },
                {
                    "width" : "15%",
                    "render" : function(data, type, row, meta) {
                        return "<button type=\"button\" class=\"btn blue btn_group_list\">详情</button>";                       
                    }
                } 
            ],
            "language" : {
                "lengthMenu" : "每页显示 _MENU_ 条记录",
                "emptyTable" : "暂无数据记录",
                "info" : "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
                "zeroRecords" : "没有搜索到匹配记录",
                "infoEmpty" : "暂无数据",
                "infoFiltered" : "",
                "processing" : "数据加载中"
            },
            "destroy" : true,
            "pageLength" : 10,
            "order" : [ [ 2, "desc" ] ],
            "columnDefs" : [ {
                "orderable" : false,
                "targets" : [ 0,1,4]
            } ],
            "dom" : "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>"
                    + "<t>" + "<'table_b relative'<'col-md-5'i><'col-md-7'>lp>",
            "fnInitComplete" : function() {
            }
        });

        //查看详情
        table.on('click', '.btn_group_list', function(event) {
            var oData_arr = table.DataTable().rows($(this).parents("tr")).data(); // 操作行对象
            var _id = oData_arr[0].unigroupId;
            var member_table = $('#member_list_table');
            member_table.dataTable({
                "processing" : true,
                 "searching" : true,  
                serverSide : false,// 开启服务器模式
                "ajax" : {
                    url : member_url,  // 列表查询
                    data:{'id':_id}
                },
                "autoWidth" : false,
                "columns" : [
                    {"width" : "15%","data" : "orderNO"},
                    {"width" : "5%","data" : "name"},
                    {"width" : "10%","data" : "mobile"},
                    {
                        "width" : "5%",
                        "data" : "total_count",
                    },
                    {
                        "width" : "5%",
                        "data" : "total",
                        "render" : function(data, type, row, meta) {
                            return data/100;
                        }
                    },
                    {"width" : "10%","data" : "create_time"},
                    {
                        "width" : "10%",
                        "data" : "null",
                        "render" : function(data, type, row, meta) {
                            if(row.id == row.unigroupId) {
                                return '团长';
                            } else {
                                return '参团';
                            }
                        }},
                ],
                "language" : {
                    "lengthMenu" : "每页显示 _MENU_ 条记录",
                    "emptyTable" : "暂无数据记录",
                    "info" : "显示 _START_ 至 _END_ 条数据，共 _TOTAL_ 条记录！",
                    "zeroRecords" : "没有搜索到匹配记录",
                    "infoEmpty" : "暂无数据",
                    "infoFiltered" : "",
                    "processing" : "数据加载中"
                },
                "destroy" : true,
                "pageLength" : 5,
                "lengthMenu": [[5, 15, 20, 50, 100], [5, 15, 20, 50, 100]],
                "order" : [ [ 5, "asc" ] ],
                "columnDefs" : [ {
                    "orderable" : false,
                    "targets" : [ 0,1,2,3,4,6]
                } ],
                "dom" : "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>"
                        + "<t>" + "<'table_b relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete" : function() {
                }
            });
            $('#groupList').modal('show');
        });


    }

    return {
        init : function() {
            _init();
        }
    };

}();

$(function() {
    roleManage.init();
});

function timestampToTime(timestamp) {
    var date = new Date(timestamp * 1000);
    Y = date.getFullYear() + '-';
    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    D = date.getDate() + ' ';
    h = checkTime(date.getHours()) + ':';
    m = checkTime(date.getMinutes()) + ':';
    s = checkTime(date.getSeconds());
    return Y+M+D+h+m+s;
}

function checkTime(i){  //补足前导0
    if (i<10)
        i="0" + i
    return i
}

function changeStore(e) {  //切换门店
    var id = e.value;
    window.location.href=index_url+'?storeId='+id;
}

$('#search #doQuery').click(function(event){  //所有拼团列表-商品名称搜索
    var name = $("#search input[name='title']").val();
    var param={"name":name};
    console.log(param);
    $('#table_record').DataTable().search(JSON.stringify(param)).draw();
});

$('#search2 .form-control').keyup(function(event){  //根据订单号或手机号查询拼团详情记录
    var order =   $("#search2 input[name='order']").val();
    var phone =   $("#search2 input[name='phone']").val();
    $('#member_list_table').DataTable().columns(0).search(order,true,false).columns(2).search(phone,true,false).draw();//对第一列订单号和第三列手机号进行模糊非智能搜索 
});




